package com.lei.dao.impl;

import com.lei.dao.RoleDao;
import com.lei.domain.Role;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @ClassName RoleDaoImpl
 * @Description TODO
 * @Author Admin
 * @Date 2021/9/15 10:33
 * @Version 1.0
 */
public  class RoleDaoImpl implements RoleDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Role> findAll() {
        List<Role> roleList = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    public void save(Role role) {
        jdbcTemplate.update("insert into sys_role value (?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    public List<Role> findRoleByUserId(Long id) {
        List<Role> roles = jdbcTemplate.query("select * from sys_user_role ur,sys_role r where ur.roleid=r.id and ur.userid=?", new BeanPropertyRowMapper<Role>(Role.class), id);

        return roles;
    }

    public void delRoleUserRel(Long roleId) {
        jdbcTemplate.update("delete from sys_user_role where roleid=?",roleId);
    }

    public void delRole( Long roleId) {
        jdbcTemplate.update("delete from sys_role where id=?",roleId);
    }


}
